Skip to content

根据图片搜索字体 - SearchFontByImage

函数简介

根据图片与已知文本,在候选字号、样式范围内搜索最匹配的系统字体与字号。

接口名称

SearchFontByImage

DLL调用

c
string SearchFontByImage(long ola, string imagePath, string knownText, string candidateFonts,
                         int minFontSize, int maxFontSize, int fontStyleMask, int topN);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
imagePath字符串图片路径。
knownText字符串图片中已知的文本内容。
candidateFonts字符串候选字体名列表,使用 | 分隔;为空则自动枚举系统字体。
minFontSize整数型最小字号。
maxFontSize整数型最大字号。
fontStyleMask整数型字体样式位掩码,可组合,见下表。
topN整数型返回候选结果条数上限。

fontStyleMask 字体样式

将需要的样式 按位或(|)相加 后传入 fontStyleMask 参数(对应 Windows GDI 字体样式标志):

说明
0常规(无特殊样式)
1粗体
2斜体
4下划线
8删除线

位运算组合示例

csharp
// 搜索粗体 + 斜体字体
int fontStyleMask = 1 | 2;  // 3
string result = ola.SearchFontByImage(@"C:\text.png", "Hello", "Arial|微软雅黑", 8, 72, fontStyleMask, 5);
cpp
// 搜索粗体 + 下划线字体
int fontStyleMask = 1 | 4;  // 5
auto result = ola.SearchFontByImage("C:\\text.png", "Hello", "Arial", 8, 72, fontStyleMask, 5);
python
# 搜索全部样式(粗体+斜体+下划线+删除线)
font_style_mask = 1 | 2 | 4 | 8  # 15
result = ola.SearchFontByImage(r"C:\text.png", "Hello", "Arial|微软雅黑", 8, 72, font_style_mask, 5)

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
std::string result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
result := ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0)
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let result = ola.search_font_by_image("C:\\test.txt", "value", "value", 0, 0, 0, 0);
cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0)
text
.局部变量 ola, OLAPlug
ola.创建 ()
result = ola.SearchFontByImage(“C:\\test.txt”, “value”, “value”, 0, 0, 0, 0)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
std::string result = ola.SearchFontByImage("C:\\test.txt", "value", "value", 0, 0, 0, 0);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = SearchFontByImage(instance, "C:\\test.txt", "value", "value", 0, 0, 0, 0);
if (ptr != 0) {
    char buffer[512] = {0};
    GetStringFromPtr(ptr, buffer, sizeof(buffer));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int SearchFontByImage(long ola, string imagePath, string knownText, string candidateFonts, int minFontSize, int maxFontSize, int fontStyleMask, int topN);

long instance = CreateCOLAPlugInterFace();
long ptr = SearchFontByImage(instance, "C:\\test.txt", "value", "value", 0, 0, 0, 0);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string result = sb.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.SearchFontByImage(instance, "C:\\test.txt", "value", "value", 0, 0, 0, 0)
if ptr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(ptr, buf, 512)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

返回值

返回值说明
(返回值)字符串型:JSON,通常包含 bestFontbestSizebestScoretop 等字段(以实际返回为准)。